package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
  * @Author yangzhongkun
  * @Description 天银租赁
  * @Date 2020/7/13 11:58
  * @return
  **/
@Controller
@RequestMapping("/institution/tyfl")
public class PrjBscInfoInsTyflController {

    /**
     * 自然人信息
     */
    @Autowired
    private ICstNpBscInfoService cstNpBscInfoService;


    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO save(HttpServletRequest request) throws BaseException {

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功");
        //客户id
        String cstId = request.getParameter("cstId");
        //居住地详细地址(取通讯地址)
        String postAddr = request.getParameter("postAddr");
        //居住地-省(取通讯地址)
        String postProvince = request.getParameter("postProvince");
        //居住地-市(取通讯地址)
        String postCity = request.getParameter("postCity");

        try{
            if(StringUtils.isEmpty(cstId)){
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "系统异常,客户id不能为空!");
            }
            //根据客户id查询 客户信息
            BaseLogUtils.newLogger("天银租赁信息采集根据cstId查询自然人信息Start:cstId={}",cstId).info();
            CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(Long.valueOf(cstId));
            BaseLogUtils.newLogger("天银租赁信息采集根据cstId查询自然人信息End:cstId={},cstNpBscInfoVO={}",cstId, JSONObject.toJSONString(cstNpBscInfoVO)).info();
            CstNpBscInfoVO npBscInfoVO = new CstNpBscInfoVO();
            if (null != cstNpBscInfoVO) {
                //修改标识
                boolean flag = false;
                npBscInfoVO.setId(cstNpBscInfoVO.getId());
                //通讯地址-省
                if (!Objects.equals(postProvince, cstNpBscInfoVO.getPostProvince())) {
                    npBscInfoVO.setPostProvince(postProvince);
                    flag = true;
                }
                //通讯地址-市
                if (!Objects.equals(postCity, cstNpBscInfoVO.getPostCity())) {
                    npBscInfoVO.setPostCity(postCity);
                    flag = true;

                }
                //居住地详细地址
                if (!Objects.equals(postAddr, cstNpBscInfoVO.getPostAddr())) {
                    npBscInfoVO.setPostAddr(postAddr);
                    flag = true;
                }
                //有信息变更 更新变更时间和变更人
                if (flag) {
                    npBscInfoVO.setMdfTm(new Date());//更新时间
                    npBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());//更新人
                }
            }

            BaseLogUtils.newLogger("天银租赁信息采集保存自然人信息Start:npBscInfoVO={}", JSON.toJSONString(cstNpBscInfoVO)).info();
            boolean modify = this.cstNpBscInfoService.modify(npBscInfoVO);
            BaseLogUtils.newLogger("天银租赁信息采集保存自然人信息End:cstId={},modifyResult={}", cstId,modify).info();
        }catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("天银租赁信息采集保存自然人信息或校验失败,cstId={},errorResult={}",cstId,ex.getMessage()).info();
            BaseLogUtils.newLogger("天银租赁信息信息采集保存自然人信息或校验失败,cstId={},errorResult={}",cstId,ex.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);

        } catch (Exception e) {
            BaseLogUtils.newLogger("天银租赁信息采集保存自然人信息或校验失败,cstId={},errorResult={}",cstId,e.getMessage()).info();
            BaseLogUtils.newLogger("天银租赁信息信息采集保存自然人信息或校验失败,cstId={},errorResult={}",cstId,e.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "保存失败，系统异常");
        }
            return result;
    }

}
